<?php
function bpt_admin_pages_on_load() {
	// Configure tab
	add_meta_box( 'bpt-admin-metaboxes-config', __( 'Ticketing Category', 'bpt' ), 'bpt_admin_screen_ticketcategory', 'store_page_wpsc-buddypressticketing-settings', 'normal', 'core' );

	// Stats
	add_meta_box( 'bpt-admin-metaboxes-stats', __( 'Todo', 'bpt' ), 'bpt_admin_screen_statistics', 'store_page_wpsc-buddypressticketing', 'normal', 'core' );
}

/**
 * bpt_admin_register_settings
 *
 * Required to utilise WordPress options API.
 */
function bpt_admin_register_settings() {
	register_setting( 'bpt-settings-group', 'bpt', 'bpt_admin_validate' );
}

/**
 * bpt_admin_validate
 *
 * Validation function for register_setting.
 */
function bpt_admin_validate( $new_settings ) {
	if ( is_string( $new_settings ) )  // wpmu-edit.php
		return get_blog_option( BP_ROOT_BLOG, 'bpt' );

	if ( isset( $new_settings['ticket_category'] ) )
			$new_settings['ticket_category'] = apply_filters( 'bpt_admin_validate_ticket_category', $new_settings['ticket_category'] );

	return serialize( wp_parse_args( $new_settings, get_blog_option( BP_ROOT_BLOG, 'bpt' ) ) );
}


// Add "Settings" link on plugins menu
function bpt_admin_add_action_link( $links, $file ) {
	if ( 'bp-ticketing/loader.php' != $file )
		return $links;

	$settings_link = '<a href="' . admin_url( 'admin.php?page=wpsc-buddypressticketing' ) . '">' . __( 'Settings', 'bpt' ) . '</a>';
	array_unshift( $links, $settings_link );

	return $links;
}
add_filter( 'plugin_action_links', 'bpt_admin_add_action_link', 10, 2 );

/**
 * bpt_admin()
 *
 * Admin page for the BuddyPress-specific settings.
 */
function bpt_admin() {
	$settings = get_blog_option( BP_ROOT_BLOG, 'bpt' );

	if ( isset( $_GET['tab'] ) && BPT_ADMIN_SETTINGS_SLUG == $_GET['tab'] )
		$is_settings_tab = true;
?>
	<div id="bp-admin">
	<div id="bpt-admin-metaboxes-general" class="wrap">

		<div id="bp-admin-header">
			<h3><?php _e( 'BuddyPress', 'bpt' ) ?></h3>
			<h4><?php _e( 'WP e-Commerce Ticketing', 'bpt' ) ?></h4>
		</div>

		<div id="bp-admin-nav">
			<ol>
				<li <?php if ( !$is_settings_tab ) echo 'class="current"' ?>><a href="<?php echo site_url( 'wp-admin/admin.php?page=wpsc-buddypressticketing', 'admin') ?>"><?php _e( 'Statistics', 'bpt' ) ?></a></li>
				<li <?php if ( $is_settings_tab ) echo 'class="current"' ?>><a href="<?php echo site_url( 'wp-admin/admin.php?page=wpsc-buddypressticketing&amp;tab=' . BPT_ADMIN_SETTINGS_SLUG, 'admin')  ?>"><?php _e( 'Configure', 'bpt' ) ?></a></li>
			</ol>
		</div>

		<?php if ( isset( $_GET['updated'] ) ) : ?>
		<div id="message" class="updated">
			<p><?php _e( 'Your WP e-Commerce Ticketing settings have been saved.', 'bpt' ) ?></p>
		</div>
		<?php endif; ?>

		<form method="post" action="options.php" id="wpsc-buddypressticketing">
			<?php wp_nonce_field( 'closedpostboxes', 'closedpostboxesnonce', false ) ?>
			<?php wp_nonce_field( 'meta-box-order', 'meta-box-order-nonce', false ) ?>
			<?php settings_fields( 'bpt-settings-group' ) ?>

			<div id="poststuff" class="metabox-holder">
				<div id="post-body" class="has-sidebar">
					<div id="post-body-content" class="has-sidebar-content">
						<?php
						if ( $is_settings_tab )
							do_meta_boxes( 'store_page_wpsc-buddypressticketing-settings', 'normal', $settings );
						else
							do_meta_boxes( 'store_page_wpsc-buddypressticketing', 'normal', $settings );
						?>
					</div>

					<?php if ( $is_settings_tab ) : ?>
						<p><input type="submit" class="button-primary" value="<?php _e( 'Save WP e-Commerce Ticketing settings', 'bpt' ) ?>" /></p>
					<?php endif ?>
				</div>
			</div>
		</form>

	</div><!-- #bpt-admin-metaboxes-general -->
	</div><!-- #bp-admin -->
<?php
}

function bpt_admin_screen_ticketcategory( $settings ) {
	$categories = bpt_wpsc_get_categories();
?>
	<p><label for="ticket_category"><?php _e( "Select the category to use for ticketing:", 'bpt' ) ?></label></p>

	<select name="bpt[ticket_category]">
	<?php
	foreach( (array)$categories as $cat ) {
		$selected = '';
		if ( $settings['ticket_category'] && $settings['ticket_category'] == $cat['id'] )
			$selected = "selected='selected'";

		echo "<option value='" . $cat['id']. "' {$selected} >" . $cat['name'] . "</option>";
	}
	?>
	</select>
<?php
}

function bpt_admin_screen_statistics( $settings ) {
?>
	<p>This will be similar to the "stats" tikipress section, but it has not been written yet.</p>
<?php
}
?>